from qturboframe.lib.utils.by import By
from qturboframe.lib.webdriver import SeleniumError
from qturboframe.public_ui.h_ui.button import Button as HuiButton
from qturboframe.public_ui.switch_window import swith_to_new_window,switch_to_original_window

from BladeFerm.Lib.Common import logger


class Button(HuiButton):
    '''hui的按钮操作'''

    def __init__(self, element_para, father=None, timeout=5, index=1):
        keys = list(element_para.keys())[0]
        if isinstance(element_para, dict) and By.TEXT in keys:
            xpath_button = ".//button[normalize-space(text())='{}' and not(contains(@style,'display: none'))]".format(
                element_para.get(By.TEXT))
            xpath_a = ".//a[contains(normalize-space(text()),'{}') and not(contains(@style,'display: none')) or normalize-space(@title)='{}']".format(
                element_para.get(By.TEXT), element_para.get(By.TEXT))
            xpath_button_span = ".//button/span[@title='{}' or normalize-space(text())='{}']/..".format(
                element_para.get(By.TEXT), element_para.get(By.TEXT))
            xpath_input = ".//input[@value='{}']".format(element_para.get(By.TEXT))
            xpath_label = ".//label[contains(text(),'{}')]".format(element_para.get(By.TEXT))
            xpath_div = ".//div[contains(text(),'{}')]".format(element_para.get(By.TEXT))
            xpath = xpath_button + " | " + xpath_a + " | " + xpath_button_span + " | " + xpath_input + " | " + xpath_label + " | " + xpath_div
            buttons = father.find_elements_by_attribute(By.XPATH, xpath, timeout=timeout)
            if len(buttons) > 0:
                self.buttons = buttons
            else:
                # 在上面查找button中包含span的控件，当没找到合适的控件后，查找符合要求的span控件
                xpath_span = ".//span[@title='{}' or normalize-space(text())='{}']".format(element_para.get(By.TEXT),
                                                                                           element_para.get(By.TEXT))
                buttons = father.find_elements_by_attribute(By.XPATH, xpath_span, timeout=timeout)
            if len(buttons) != 0:
                super().__init__(buttons[index - 1], father, timeout)
                return
            raise SeleniumError("没有找到名字为'{}'的按钮".format(element_para))
        elif isinstance(element_para, dict) and By.TEXT not in keys and By.XPATH in keys:
            buttons = father.find_elements_by_attribute(By.XPATH, element_para.get("xpath"), timeout=timeout)
            if len(buttons) > 0:
                self.buttons = buttons
            else:
                raise SeleniumError("没有找到名字为'{}'的按钮".format(element_para))
            if len(buttons) != 0:
                super().__init__(buttons[index - 1], father, timeout)
                return
        else:
            super().__init__(element_para, father, timeout)

    @swith_to_new_window(10)
    def click_to_new_window(self):
        '''
        omc按钮点击弹出切换新窗口

        :Args:
         - inputdata: 无

        :Returns:
            无

        :Raises:
            SeleniumError: 无
        '''
        self.click()

    @switch_to_original_window
    def click_back_to_default_window(self):
        '''
        omc按钮点击返回旧窗口

        :Args:
         - inputdata: 无

        :Returns:
            无

        :Raises:
            SeleniumError: 无
        '''
        self.click()